package com.markerhub.modules.app.interceptor;

import com.markerhub.common.exception.HubException;
import com.markerhub.common.lang.Const;
import com.markerhub.modules.app.annotation.Login;
import com.markerhub.utlis.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;


@Component
public class AuthInterceptor implements HandlerInterceptor {

    @Resource
    JwtUtils jwtUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       // 判断是否有@Login注解，没有就放行
        Login annotation = null;
        if (handler instanceof HandlerMethod){
            annotation=((HandlerMethod) handler).getMethodAnnotation(Login.class);
        }else{
            return true;

        }

        if (annotation==null){
            return true;
        }

        ///获取用户凭证
        String token = request.getHeader("token");
        // 判断token是否合法
        if (!StringUtils.hasText(token)) {
            throw new HubException("请先登录");
        }

        Claims claim = jwtUtils.getClaimByToken(token, "APP");
        if (claim == null || jwtUtils.isTokenExpired(claim.getExpiration())) {
            throw new HubException("请先登录");
        }

        // 把用户信息存到session
        request.getSession().setAttribute(Const.USER_KEY, Long.parseLong(claim.getSubject()));

        ///判断token是否合法
        return true;
    }
}
